/***********************************************************************
** Copyright (C) 2011 Anthony Naddeo <anthony.naddeo@gmail.com>
**
** This file is part of qtweather-qml
**
** qtweather-qml is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 3 of the License, or
** (at your option) any later version.
**
** qtweather-qml is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this package; If not, see <http://www.gnu.org/licenses/>.
**
***********************************************************************/

import QtQuick 1.1
import com.meego 1.0
import com.meego.extras 1.0 //for info banner

Page {
    id: mainPage
    tools: commonTools
	orientationLock: Page.lockInPortrait

	signal finished(string zipcode)
	property bool busy: weatherModel.status == XmlListModel.Ready || weatherModel.status == XmlListModel.Error ? false : true
	property string zipcode
	property string title
	property string hazards

	property Component settings: settingsComp
	property alias settingsLoader: loader

	onZipcodeChanged: weatherModel.source = "http://umcs.maine.edu/~naddeoa/qtweather-qml?zipcode=" + zipcode

	Component{
		id: settingsComp
		Settings{
			visualParent: mainPage.pageStack
			onSelected: mainPage.zipcode = zipcode
			Connections{
				target: mainPage
				onFinished: update(zipcode)
			}
		}
	}


	Loader{id: loader; anchors.fill: parent}
	Timer{
		interval: 250
		running: true
		onTriggered: loader.sourceComponent = settings
	}



	//present a warning for hazards
	function warn(warningText){
		var warning = banner.createObject(mainPage)
		warning.text = warningText
		warning.show()
		warning.destroy(20000)
	}
	Timer{
		id: bannerTimer
		property string warning
		onWarningChanged: start() //triggered in weatherModel
		interval: 1000
		onTriggered: warn(warning)
	}

	Component{
		id: banner
		InfoBanner{
			id: info
			text: weatherModel.get(0).hazards
			timerEnabled: true
			//iconSource: "image://theme/icon-m-conversation-contact-attention-inverse"
			iconSource: "image://theme/icon-s-email-priority"
			timerShowTime: 10000
//			Component.onCompleted: {show(); info.destroy(timerShowTime*2)}
		}
	}

	Timer{//update the app every 4 hours
		running: zipcode ?  true : false
		interval: 1440000
		repeat: true
		onTriggered: weatherModel.reload()
	}

	WeatherModel{
		id: weatherModel
		onStatusChanged: {
			if(status === XmlListModel.Ready && get(0)){
				mainPage.title = get(0).city
				if(get(0).hazards)
					bannerTimer.warning = get(0).hazards
				Utilities.setHome(zipcode,get(0).city)
				mainPage.finished(zipcode)
			}

		}
	}



	ListView{
		id: list
		orientation: ListView.Horizontal
		anchors.fill: parent
		snapMode: ListView.SnapToItem
		flickDeceleration: 10000
		model:weatherModel
		cacheBuffer: 10000
		highlightRangeMode: ListView.StrictlyEnforceRange
		preferredHighlightBegin: 0
		preferredHighlightEnd:0

		section.property:  "day"
		section.criteria: ViewSection.FullString


		SectionScroller{
			listView: list
		}

//		ScrollDecorator{
//			flickableItem: list
//		}

		delegate: DayDelegate{
			height: list.height
			width: list.width
			shown: index === list.currentIndex
			animateRunning: loader.item.animateRunning
		}
	}
}
